package student.action;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.util.MyLog;

import edu.student.models.Lookup;
/**
 * 
 * @author Sai Naga Sravani Peraka
 */
public class AddLookup extends ActionSupport{
	private int lookup_id=0;
	int i=0;
	private String table="";
	private String name = "";
	private String is_current = "";
	private String submit="";
	private String selectionModifier="";
	private String column="";
	private int interest_id;
	private int faculty_id;
	private String subject_code="";
	private String duration="";
	private ArrayList<String> interestList = new ArrayList<String>();
	private ArrayList<String> facultyList = new ArrayList<String>();
	Lookup lookup =new Lookup();
    private ArrayList<Lookup> allValues = new ArrayList<Lookup>();
	public String execute() throws Exception {
	
		Map session = ActionContext.getContext().getSession();
		session = ActionContext.getContext().getSession();
		table=(String)session.get("table");
		System.out.println("here"+session.get("table"));
		MyLog.log("in AddLookup.execute() with  = ");
		if (this.name.isEmpty()) {
			if(table.equals("announcement")){
				
				column=",interest_id";
				this.allValues=lookup.find("interest"," where is_current='y'");
				
				for (int i = 0; i < allValues.size(); i++){
		        	Lookup list = allValues.get(i);
		        	interestList.add(list.getName());
		        	MyLog.log("name:"+list.getName());
		        	System.out.println("interestname:"+list.getName());
		        }
				return "announcement";
			}
			if(table.equals("news")){
				
				column=",interest_id";
				this.allValues=lookup.find("interest"," where is_current='y'");
				
				for (int i = 0; i < allValues.size(); i++){
		        	Lookup list = allValues.get(i);
		        	interestList.add(list.getName());
		        	MyLog.log("name:"+list.getName());
		        	System.out.println("interestname:"+list.getName());
		        }
				return "news";
			}
			if(table.equals("subject")){
				
				column=",faculty_id";
				this.allValues=lookup.find("faculty"," where is_current='y'");
				
				for (int i = 0; i < allValues.size(); i++){
		        	Lookup list = allValues.get(i);
		        	facultyList.add(list.getName());
		        	MyLog.log("name:"+list.getName());
		        	System.out.println("facultyname:"+list.getName());
		        }
				return "subject";
			}
			if(table.equals("semester")){
				return "semester";
			}
			return "initial";
		}
		else{
		lookup.setTable(table);
		lookup.setId(lookup_id);
		lookup.setName(name);
		lookup.setIs_current(is_current);
		lookup.setSubject_code(subject_code);
		if(table.equals("announcement")){
			
			column=",interest_id,duration";
			HttpServletRequest request=(HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
			 
	        Enumeration paramNames= request.getParameterNames();
	 
	        while(paramNames.hasMoreElements())
	        {
	            String param=(String)paramNames.nextElement();
	            MyLog.log("\nParameter Name : "+param);
	            MyLog.log("Parameter Values : ");
	             if(param.equals("myInterest")){
	            	 String[] paramValues=request.getParameterValues(param);
		             
	 	            for(String paramVal:paramValues)
	 	            {
	 	            	Lookup interestList=Lookup.findOne("interest", "where interest_name = '"+paramVal+"'");
	 	            	interest_id=interestList.getId();
	 	            	System.out.println("interestid:"+interest_id);
	 	                MyLog.log(paramVal);
	 	            }
	             }
	        }
			selectionModifier=", "+interest_id+", '"+duration+"'";
		}
		if(table.equals("news")){
			
			column=",interest_id,duration";
			HttpServletRequest request=(HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
			 
	        Enumeration paramNames= request.getParameterNames();
	 
	        while(paramNames.hasMoreElements())
	        {
	            String param=(String)paramNames.nextElement();
	            MyLog.log("\nParameter Name : "+param);
	            MyLog.log("Parameter Values : ");
	             if(param.equals("myInterest")){
	            	 String[] paramValues=request.getParameterValues(param);
		             
	 	            for(String paramVal:paramValues)
	 	            {
	 	            	Lookup interestList=Lookup.findOne("interest", "where interest_name = '"+paramVal+"'");
	 	            	interest_id=interestList.getId();
	 	            	System.out.println("interestid:"+interest_id);
	 	                MyLog.log(paramVal);
	 	            }
	             }
	        }
			selectionModifier=", "+interest_id+", '"+duration+"'";
		}
		if(table.equals("semester")){
			column=",duration";
			selectionModifier=", '"+duration+"'";
		}
		else if(table.equals("subject")){
			
			column=",faculty_id,subject_code";
			HttpServletRequest request=(HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
			 
	        Enumeration paramNames= request.getParameterNames();
	 
	        while(paramNames.hasMoreElements())
	        {
	            String param=(String)paramNames.nextElement();
	            MyLog.log("\nParameter Name : "+param);
	            MyLog.log("Parameter Values : ");
	             if(param.equals("myFaculty")){
	            	 String[] paramValues=request.getParameterValues(param);
		             
	 	            for(String paramVal:paramValues)
	 	            {
	 	            	Lookup facultyList=Lookup.findOne("faculty", "where faculty_name = '"+paramVal+"'");
	 	            	faculty_id=facultyList.getId();
	 	            	System.out.println("facultyid:"+faculty_id);
	 	                MyLog.log(paramVal);
	 	            }
	             }
	        }
			selectionModifier=", "+faculty_id+",'"+subject_code+"'";
		}
		else if(table.equals("faculty")){
			column=",email_id";
			String email_id = lookup.getName()+"@iiitb.org";
			selectionModifier=", '"+email_id+"'";
		}
		lookup.insert(table,column,selectionModifier);
		System.out.println(lookup_id+" lookup details are successfully updated...!");
		this.allValues = lookup.find(table,"");
		return "success";
		}
	}
	public int getLookup_id() {
		return lookup_id;
	}
	public void setLookup_id(int lookup_id) {
		this.lookup_id = lookup_id;
	}
	public String getTable() {
		return table;
	}
	public void setTable(String table) {
		this.table = table;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getIs_current() {
		return is_current;
	}
	public void setIs_current(String is_current) {
		this.is_current = is_current;
	}
	public String getSubmit() {
		return submit;
	}
	public void setSubmit(String submit) {
		this.submit = submit;
	}
	public Lookup getLookup() {
		return lookup;
	}
	public void setLookup(Lookup lookup) {
		this.lookup = lookup;
	}
	public ArrayList<Lookup> getAllValues() {
		return allValues;
	}
	public void setAllValues(ArrayList<Lookup> allValues) {
		this.allValues = allValues;
	}
	public int getInterest_id() {
		return interest_id;
	}
	public void setInterest_id(int interest_id) {
		this.interest_id = interest_id;
	}
	public ArrayList<String> getInterestList() {
		return interestList;
	}
	public void setInterestList(ArrayList<String> interestList) {
		this.interestList = interestList;
	}
	public ArrayList<String> getFacultyList() {
		return facultyList;
	}
	public void setFacultyList(ArrayList<String> facultyList) {
		this.facultyList = facultyList;
	}
	public String getSubject_code() {
		return subject_code;
	}
	public void setSubject_code(String subject_code) {
		this.subject_code = subject_code;
	}
	public String getDuration() {
		return duration;
	}
	public void setDuration(String duration) {
		this.duration = duration;
	}
}
